package dao.mysql;

import java.sql.*;
import javax.naming.*;
import javax.sql.*;

public class MyJNDIConnection {
	public static String DATASOURCE_CONTEXT = "java:comp/env/jdbc/SysAdmin";
	
	public Connection getJNDIConnection(){
		Connection conn = null;
		try{
			Context initialContext = new InitialContext();
			if ( initialContext == null)
				System.out.println("JNDI problem. Cannot get InitialContext.");
			
			DataSource datasource = (DataSource) initialContext.lookup(DATASOURCE_CONTEXT);
			if ( datasource != null)
				conn = datasource.getConnection();
			else
				System.out.println("Failed to lookup datasource.");
		}
		catch(NamingException ex){
			System.out.println("Naming Exception problem");
		}
		catch(SQLException ex){
			System.out.println("SQL Exception problem");
		}
		return conn;
	}
}
